package com.liusy.code.templates;

import java.util.List;
import com.liusy.code.bean.*;

public class JspFormTemplate
{
  protected static String nl;
  public static synchronized JspFormTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    JspFormTemplate result = new JspFormTemplate();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = NL + "<%@ page pageEncoding=\"UTF-8\"%>" + NL + "<form id=\"";
  protected final String TEXT_2 = "Form\">" + NL + "\t\t\t\t<table class=\"formStyle\">" + NL + "\t\t\t" + NL + "\t\t\t";
  protected final String TEXT_3 = NL + "\t\t\t\t<tr>" + NL + "\t\t\t\t\t <td class=\"formItem\" ><span class=\"redStar\">*</span>";
  protected final String TEXT_4 = "：</td>" + NL + "\t\t\t         <td ><input type=\"text\" name=\"";
  protected final String TEXT_5 = ".";
  protected final String TEXT_6 = "\" onFocus=\"WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})\" value=\"${fn:substring(";
  protected final String TEXT_7 = ".";
  protected final String TEXT_8 = ", 0, 19)}\" readonly=\"readonly\" class=\"Wdate\"/></td>" + NL + "\t\t\t\t</tr>" + NL + "\t\t\t\t";
  protected final String TEXT_9 = NL + "\t\t\t\t\t<tr>" + NL + "\t\t\t\t\t <td class=\"formItem\" ><span class=\"redStar\">*</span>";
  protected final String TEXT_10 = "：</td>" + NL + "\t\t\t         <td ><input type=\"text\" name=\"";
  protected final String TEXT_11 = ".";
  protected final String TEXT_12 = "\" class=\"width_100p\" value=\"${";
  protected final String TEXT_13 = ".";
  protected final String TEXT_14 = "}\" /></td>" + NL + "\t\t\t\t\t</tr>" + NL + "\t\t\t";
  protected final String TEXT_15 = NL + "\t\t\t\t\t</table>" + NL + "\t\t\t\t\t<c:if test=\"${requestScope.type eq 'edit' }\">" + NL + "\t\t\t\t\t\t<input type=\"hidden\" name=\"";
  protected final String TEXT_16 = ".id\" value=\"${";
  protected final String TEXT_17 = ".id }\" />" + NL + "\t\t\t\t\t</c:if>" + NL + "</form>";

  public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
	JspTemplateArgs args = (JspTemplateArgs)argument;
	
	String modeName = args.getModeName();
	String entityName = args.getEntityName();
	String jspName = args.getJspName();
	
	List<EntityFieldInfo> entityFieldInfoList = args.getEntityFieldInfoList();
		List<ColumnInfo> columnInfoList = args.getColumnInfoList();
	
	String entityPackageName = args.getPackageNameInfo().getEntityPackageName();
	String daoPackageName = args.getPackageNameInfo().getDaoPackageName();
	String servicePackageName = args.getPackageNameInfo().getServicPackageName();
	String actionPackageName = args.getPackageNameInfo().getActionPackageName();
	
	String entityClassName = args.getClassNameInfo().getEntityClassName();
	String daoClassName = args.getClassNameInfo().getDaoClassName();
	String serviceClassName = args.getClassNameInfo().getServicClassName();
	String actionClassName = args.getClassNameInfo().getActionClassName();
	
	String entityObjectName = args.getClassNameInfo().getEntityObjectName();
	String daoObjectName = args.getClassNameInfo().getDaoObjectName();
	String serviceObjectName = args.getClassNameInfo().getServicObjectName();
	String actionObjectName = args.getClassNameInfo().getActionObjectName();
	String jsCode="";
	StringBuffer temp = new StringBuffer();
	for(EntityFieldInfo entityFieldInfo : entityFieldInfoList)
	{
		List<String> fieldNames = entityFieldInfo.getFieldNames();
		
		for(String fieldName : fieldNames)
		{
			temp.append(fieldName+":\"required\",");		
		}
	}
	jsCode = temp.substring(0,temp.length()-1);

    stringBuffer.append(TEXT_1);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_2);
    
			for(ColumnInfo columnInfo : columnInfoList)
			{
				if(columnInfo.getNameClassl().equals("id"))
				{
					continue;
				}
				
				if(columnInfo.getJavaType().equals("Date"))
				{
				
				
    stringBuffer.append(TEXT_3);
    stringBuffer.append(columnInfo.getRemark());
    stringBuffer.append(TEXT_4);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_5);
    stringBuffer.append(columnInfo.getNameClassl());
    stringBuffer.append(TEXT_6);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_7);
    stringBuffer.append(columnInfo.getNameClassl());
    stringBuffer.append(TEXT_8);
    
					continue;
				}
			
    stringBuffer.append(TEXT_9);
    stringBuffer.append(columnInfo.getRemark());
    stringBuffer.append(TEXT_10);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(columnInfo.getNameClassl());
    stringBuffer.append(TEXT_12);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(columnInfo.getNameClassl());
    stringBuffer.append(TEXT_14);
    		
			}
			
    stringBuffer.append(TEXT_15);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_16);
    stringBuffer.append(entityObjectName);
    stringBuffer.append(TEXT_17);
    return stringBuffer.toString();
  }
}
